using UnityEngine;
using System.Collections;


public class MyGestureDetector : MonoBehaviour
{

    public delegate void OnSwipeHandler(Vector2 move, float magnitude);
    public delegate void OnDragHandler(Vector2 position);
    public delegate void OnDragEndHandler( );
    public delegate void OnTapHandler(Vector3 position);

    public static MyGestureDetector instance;
    public OnDragHandler dragHandler;
    public OnDragEndHandler dragEndHandler;
    public OnTapHandler tapHandler;
    public OnSwipeHandler swipeHandler;

    public void Awake()
    {
        instance = this;
    }

    public void RegisterDrag(OnDragHandler dragHandler)
    {
        this.dragHandler += dragHandler;
    }

    public void RegisterDragEnd(OnDragEndHandler dragEndHandler)
    {
        this.dragEndHandler += dragEndHandler;
    }

    public void RegisterSwipe(OnSwipeHandler swipeHandler)
    {
        this.swipeHandler += swipeHandler;
    }

    public void RegisterTap(OnTapHandler tapHandler)
    {
        this.tapHandler += tapHandler;
    }

    public void UnRegisterDrag(OnDragHandler dragHandler)
    {
        this.dragHandler -= dragHandler;
    }

    public void UnRegisterDragEnd(OnDragEndHandler dragEndHandler)
    {
        this.dragEndHandler += dragEndHandler;
    }
    
    public void UnRegisterSwipe(OnSwipeHandler swipeHandler)
    {
        this.swipeHandler -= swipeHandler;
    }
    
    public void UnRegisterTap(OnTapHandler tapHandler)
    {
        this.tapHandler -= tapHandler;
    }

    void OnLongPress( LongPressGesture gesture )
    {

    }

    void OnTap( TapGesture gesture )
    {
        if( InGameSetting.inputMode == InputType.FINGERTAPSWIPE)
        {
            //Debug.Log( "Tapped with finger " + gesture.Fingers[0] + "position: " + gesture.Hit.point);
            if(tapHandler != null)
            {
                tapHandler(gesture.Hit.point);
            }
        }
    }

    void OnDoubleTap( TapGesture gesture )
    {

    }

    // in real game just disable XXX.detector
    void OnSwipe( SwipeGesture gesture )
    {
        if( InGameSetting.inputMode == InputType.FINGERTAPSWIPE)
        {
            Debug.Log( "Swiped " + gesture.Direction 
                     + " with finger " + gesture.Fingers[0] + 
                      " (velocity:" + gesture.Velocity + "Move "
                      + gesture.Move + 
                     ", distance: " + gesture.Move.magnitude + " )");
            if(swipeHandler != null)
            {
                swipeHandler(gesture.Move,gesture.Move.magnitude);
            }
        }

    }

    int dragFingerIndex = -1;
    public Vector2 firstTouch;
    public Vector2 deltaTouch;
    protected float threshHold = 3f;

    void OnDrag( DragGesture gesture )
    {
        if( InGameSetting.inputMode == InputType.VIRTUALCONTROL)
        {
            // first finger
            FingerGestures.Finger finger = gesture.Fingers[0];

            if( gesture.Phase == ContinuousGesturePhase.Started )
            {
                if(gesture.Position.x > Screen.width/2 || gesture.Position.y >  Screen.height/2)
                    return;

                Debug.Log( "Phase == ContinuousGesturePhase.Started Started dragging with finger " + finger);

                // remember which finger is dragging dragObject
                dragFingerIndex = finger.Index;

                firstTouch = gesture.Position;
            }
            else if( finger.Index == dragFingerIndex )  // gesture in progress, make sure that this event comes from the finger that is dragging our dragObject
            {
                if( gesture.Phase == ContinuousGesturePhase.Updated )
                {
                    // update the position by converting the current screen position of the finger to a world position on the Z = 0 plane
                    //dragObject.transform.position = GetWorldPos( gesture.Position );

                    deltaTouch = gesture.Position - firstTouch;
                    if(gesture.Position.x < Screen.width/2 
                       && gesture.Position.y <  Screen.height/2
                       && ( Mathf.Abs(deltaTouch.x) > threshHold || Mathf.Abs(deltaTouch.y) > threshHold) )
                    {
                        //Debug.Log( "Phase == ContinuousGesturePhase.Updated " + deltaTouch);
                        deltaTouch.Normalize();
                        dragHandler(deltaTouch);
                    }
                    else
                    {
                        //dragEndHandler();
                    }
                }
                else
                {
                    Debug.Log( "Stopped dragging with finger " + finger );

                    // reset our drag finger index
                    dragFingerIndex = -1;

                    dragEndHandler();
                }
            }
        }
    }

    // Convert from screen-space coordinates to world-space coordinates on the Z = 0 plane
    public Vector3 GetWorldPos( Vector2 screenPos )
    {
        Ray ray = Camera.main.ScreenPointToRay( screenPos );
        
        // we solve for intersection with z = 0 plane
        float t = -ray.origin.z / ray.direction.z;
        
        return ray.GetPoint( t );
    }
   
}
